﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TpsAutoSurvey_PDA_Version_.Presenter;
using System.Windows.Forms;
using TpsAutoSurvey_PDA_Version_.SmsModule;
using System.Net;
using Microsoft.WindowsMobile.Status;
using TpsAutoSurvey_PDA_Version_.SocketModule;
using TpsAutoSurvey_PDA_Version_.Model;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.Win32;

namespace TpsAutoSurvey_PDA_Version_.RealizedClasses
{
    public class FrmPresenter 
    {
        SurveyInterface surveyConrol { get; set; }
        Mfrm _mfrm;
        //SmsCommand smsCmd;

        //Additional Servicemethod, only for this Service
      
        SystemState systemState = new SystemState(SystemProperty.PhoneCallTalking);

        public FrmPresenter(Mfrm frm)
        {
            _mfrm = frm;
            WireUpViewEvents();
        }

 


        private void WireUpViewEvents()
        {
            _mfrm.Load += new EventHandler(_mfrm_Load);
            //_mfrm.Closed += new EventHandler(_mfrm_Closed);
            _mfrm.btnStartTimer.Click += new EventHandler(btnStartTimer_Click);
            _mfrm.btnStopTimer.Click += new EventHandler(btnStopTimer_Click);
            _mfrm.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
            _mfrm.radioButton2.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
            _mfrm.BtnSetTimeInterval.Click += new EventHandler(BtnSetTimeInterval_Click);
            _mfrm.BtnAddCustomTime.Click += new EventHandler(BtnAddCustomTime_Click);
            _mfrm.BtnDeleteCustomTime.Click += new EventHandler(BtnDeleteCustomTime_Click);
        }

        void BtnDeleteCustomTime_Click(object sender, EventArgs e)
        {

        }

        void BtnAddCustomTime_Click(object sender, EventArgs e)
        {

     
        }


        //设置Tick模式测量
        void BtnSetTimeInterval_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("设置成功!");
            }
            catch
            {
                MessageBox.Show("发生错误!");
            }
        }
        //关闭的时候关机
        void _mfrm_Closed(object sender, EventArgs e)
        {
            surveyConrol.ShutDown();
            surveyConrol.datatrans.Geo.port.Close();
        }


        void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

            if (_mfrm.radioButton1.Checked == true)
            {
                _mfrm.BtnSetTimeInterval.Enabled = true;
                _mfrm.BtnAddCustomTime.Enabled = false;
                _mfrm.BtnDeleteCustomTime.Enabled = false;
                surveyConrol.StartTimer();

            }
            else
            {
                surveyConrol.StopSurvey();
                _mfrm.BtnSetTimeInterval.Enabled = false;
                _mfrm.BtnAddCustomTime.Enabled = true;
                _mfrm.BtnDeleteCustomTime.Enabled = true;
               
            }
        }


        void btnStopTimer_Click(object sender, EventArgs e)
        {

            _mfrm.btnStartTimer.Visible = true;
            _mfrm.btnStopTimer.Visible = false;
            _mfrm.panel1.Visible = false;
            surveyConrol.StopTimer();
        }

        void btnStartTimer_Click(object sender, EventArgs e)
        {

            _mfrm.btnStartTimer.Visible = false;
            _mfrm.btnStopTimer.Visible = true;
            //_mfrm.panel1.Visible = true;
            _mfrm.radioButton1.Checked = (_mfrm.radioButton2.Checked == true) ? false : true;
            surveyConrol.StartTimer();
        }

        void _mfrm_Load(object sender, EventArgs e)
        {
            //ConnectionBLL d = new ConnectionBLL();
            //d.Start();

            surveyConrol = ConrollerFactory.CreateController();
            surveyConrol.PrepareStation();
            surveyConrol.GetCurNo();
            //smsCmd = new SmsCommand(surveyConrol);


            //默认启动定时并且凌晨1,2,3,4点钟测量
            btnStartTimer_Click(null, null);

        }
    }

    public static class ConrollerFactory
    {
        public static SurveyInterface CreateController()
        {
            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString())
              + "\\config.txt";
            bool isdisprj = false;
            try
            {
                 isdisprj = Convert.ToBoolean(ToolClass.GetProfileString(path, "IsDistPrj"));
            }
            catch
            {
                isdisprj = false;
            }
            if (isdisprj == true)
            {
                return new SurveyDistController();
            }
            else
            {
                return new SurveyTpsController();
            }
        }
    }
}
